package view;

//SplashScreen.java
//A simple application to show a title screen in the center of the screen
//for the amount of time given in the constructor.  This class includes
//a sample main() method to test the splash screen, but it's meant for use
//with other applications.
//

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

import model.GameConfig;

public class SplashScreen extends JWindow {

	private static final long serialVersionUID = -7269385239010866684L;
	
	private GameStartConfig gsc;
	private Object			lockConfig;
	private boolean			displayConfig;

	public SplashScreen( boolean b ) {
		displayConfig = b;
		showSplash();
	}


	/**
	 * Simple little method to show a title screen in the center
	 * of the screen for the amount of time given in the constructor.
	 */
	public void showSplash() {
		lockConfig = new Object();
		
		JPanel content = (JPanel) getContentPane();
		content.setBackground(Color.white);

		// Set the window's bounds, centering the window
			int width = 600;
			int height = 200;
			Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
			int x = (screen.width - width) / 2;
			int y = (screen.height - height) / 2;
			setBounds(x, y, width, height);

		// Build the splash screen
			JLabel label = new JLabel();
			label.setIcon(new ImageIcon("images/RiskLogoSplash.png"));
			JLabel copyrt = new JLabel("CSC 335 Purple-People Eaters:  RISK!  ",
					JLabel.CENTER);
			copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
			content.add(label, BorderLayout.CENTER);
			content.add(copyrt, BorderLayout.SOUTH);
			Color oraRed = new Color(156, 20, 20, 255);
			content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
			
			this.addMouseListener(new MouseActionListener());
		
		// Display it
			setVisible(true);
		
		/*
		 * TODO Should automatically wait here for a Map to be sent to it.
		 * 		If the Config because is supposed to show up, it'll be
		 * 		called by RiskClient. Even if it is called, then it should be
		 * 		waiting for the Map to be sent back anyway.
		 */
			synchronized( lockConfig ) {
				
					System.out.println("SplashScreen: before wait");
					if(displayConfig){
						try {
							lockConfig.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}	
					}
					
					System.out.println("SplashScreen: after wait");
					dispose();
				//
			}
	}
	private class MouseActionListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			if ( displayConfig == true ) {
				displayGameConfig();
				gsc.setVisible(true);
			}
			else {
				//setVisible( false );
				// TODO This dispose call should only happen when it receives a map.
				//		Wait, should that even happen here?
				//		Wait, should both cases not dispose() until they construct gui?
			}
			
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
		
	}

	public void showSplashAndExit() {
		showSplash();
		System.exit(0);
	}
	
	/**
	 * Displays JFrame that lets users choose the options
	 * of the game (how many humans and players total). 
	 */
	private void displayGameConfig() {
		gsc = new GameStartConfig( lockConfig );
	}
	
	public GameConfig getGameConfig() {
		return gsc.getGameConfig();
	}
	
	public void unlockSplash() {
		synchronized(lockConfig){
			lockConfig.notify();
		}
	}

//	public static void main(String[] args) {
//		// Throw a nice little title page up on the screen first
//		SplashScreen splash = new SplashScreen(10000);
//		// Normally, we'd call splash.showSplash() and get on with the program.
//		// But, since this is only a test...
//		splash.showSplash();
//	}
}